<template>
	<view class="pages overflow-auto pad-b-80">
		<view class="mar-10 border-rad-3 border-type-1">
			<view
				v-for="(item, index) in tableData"
				:key="index"
				class="flex align-center"
				:style="{
					borderBottom: index !== tableData.length -1 ? '1px #e6e6e6 solid' : 'none',
				}"
			>
				<view class="flex align-center w-px-120 pad-x-10" style="min-height: 26px;">
					<text>{{ item.DESCR }}</text>
				</view>
				
				<view
					class="flex flex-1 align-center"
					:style="{
						borderLeft: '1px #e6e6e6 solid',
						minHeight: '26px',
						padding: item.TYPE !== 'textarea' ? '10px 10px' : '10px 0px',
					}"
				>
					<text v-if="item.TYPE === 'disabled'" class="color-333333">{{ item.DESCR_CVAL }}</text>
					<view v-else class="w-100">
						<view v-if="item.TYPE === 'input'">
							<view
								v-if="item.DESCR === '车号'"
								class="flex align-center"
							>
								<input
									type="text"
									v-model="item.DESCR_CVAL"
									:placeholder="item.placeholder"
									:disabled="item.isEdit"
									style="color: #333333; height: 30px;"
									@blur="handleBlur($event, item)"
								/>
								
								<view class="w-px-80 mar-l-10">
									<u-button type="primary" color="#bd3124" @click="handleSearch()">查询</u-button>
								</view>
							</view>
							
							<input
								v-else
								type="text"
								v-model="item.DESCR_CVAL"
								:placeholder="item.placeholder"
								:disabled="item.isEdit"
								style="color: #333333; height: 30px;"
							/>
						</view>
						
						<view v-else-if="item.TYPE === 'textarea'" class="w-100">
							<textarea
								v-model="item.DESCR_CVAL"
								:placeholder="item.placeholder"
								:disabled="item.isEdit"
								auto-height
								style="width: auto; color: #333333; margin: 10px; height: 30px;"
							></textarea>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="mar-10 border-rad-3 border-type-1">
			<view
				v-for="(item, index) in tableData2"
				:key="index"
				class="flex align-center"
				:style="{
					borderBottom: index !== tableData2.length -1 ? '1px #e6e6e6 solid' : 'none',
				}"
			>
				<view class="flex align-center w-px-120 pad-x-10" style="min-height: 26px;">
					<text>{{ item.DESCR }}</text>
				</view>
				
				<view
					class="flex flex-1 align-center"
					:style="{
						borderLeft: '1px #e6e6e6 solid',
						minHeight: '26px',
						padding: item.TYPE !== 'textarea' ? '10px 10px' : '10px 0px',
					}"
				>
					<text v-if="item.TYPE === 'disabled'" class="color-333333">{{ item.DESCR_CVAL }}</text>
					<view v-else class="w-100">
						<view v-if="item.TYPE === 'input'">
							<view
								v-if="item.DESCR === '车号'"
								class="flex align-center"
							>
								<input
									type="text"
									v-model="item.DESCR_CVAL"
									:placeholder="item.placeholder"
									:disabled="item.isEdit"
									style="color: #333333; height: 30px;"
									@blur="handleBlur($event, item)"
								/>
								
								<view class="w-px-80 mar-l-10">
									<u-button type="primary" color="#bd3124" @click="handleSearch()">查询</u-button>
								</view>
							</view>
							
							<input
								v-else
								type="text"
								v-model="item.DESCR_CVAL"
								:placeholder="item.placeholder"
								:disabled="item.isEdit"
								style="color: #333333; height: 30px;"
							/>
						</view>
						
						<view v-else-if="item.TYPE === 'textarea'" class="w-100">
							<textarea
								v-model="item.DESCR_CVAL"
								:placeholder="item.placeholder"
								:disabled="item.isEdit"
								auto-height
								style="width: auto; color: #333333; margin: 10px; height: 30px;"
							></textarea>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="mar-10 border-rad-3 border-type-1">
			<view
				v-for="(item, index) in tableData3"
				:key="index"
				class="flex align-center"
				:style="{
					borderBottom: index !== tableData3.length -1 ? '1px #e6e6e6 solid' : 'none',
				}"
			>
				<view class="flex align-center w-px-120 pad-x-10" style="min-height: 26px;">
					<text>{{ item.DESCR }}</text>
				</view>
				
				<view
					class="flex flex-1 align-center"
					:style="{
						borderLeft: '1px #e6e6e6 solid',
						minHeight: '26px',
						padding: item.TYPE !== 'textarea' ? '10px 10px' : '10px 0px',
					}"
				>
					<view v-if="item.TYPE === 'disabled'">
						<text v-if="item.DESCR_CVAL === 1" class="color-ff0000">{{ item.REASON }}</text>
					</view>
					
					<view v-else class="w-100">
						<view v-if="item.TYPE === 'input'">
							<view
								v-if="item.DESCR === '车号'"
								class="flex align-center"
							>
								<input
									type="text"
									v-model="item.DESCR_CVAL"
									:placeholder="item.placeholder"
									:disabled="item.isEdit"
									style="color: #333333; height: 30px;"
									@blur="handleBlur($event, item)"
								/>
								
								<view class="w-px-80 mar-l-10">
									<u-button type="primary" color="#bd3124" @click="handleSearch()">查询</u-button>
								</view>
							</view>
							
							<input
								v-else
								type="text"
								v-model="item.DESCR_CVAL"
								:placeholder="item.placeholder"
								:disabled="item.isEdit"
								style="color: #333333; height: 30px;"
							/>
						</view>
						
						<view v-else-if="item.TYPE === 'textarea'" class="w-100">
							<textarea
								v-model="item.DESCR_CVAL"
								:placeholder="item.placeholder"
								:disabled="item.isEdit"
								auto-height
								style="width: auto; color: #333333; margin: 10px; height: 30px;"
							></textarea>
						</view>
					</view>
				</view>
			</view>
		
		</view>
		
		<view v-if="pageState === 1" class="bg-dbdcf8 pad-10">
			<view class="font-14 font-b-800">
				<text>选择维修工：</text>
			</view>
			
			<view class="flex flex-wrap align-center mar-y-10">
				<CustomRadio
					class="mar-y-5 mar-r-10"
					v-for="(item, index) in repairWorkers"
					:groudId="1"
					:key="index"
					:iconType="'radio'"
					:label="item.RealName"
					:name="item.UserName"
					:value="item.Checked"
					:in-active-value="1"
					:active-value="0"
					@handleRadioClick="handleRadioClick"
				/>
			</view>
		</view>
		
		<view
			v-if="pageState === 1"
			class="flex align-center pos-f bg-white pad-x-10 h-px-60 shadow-type-3"
			style="left: 0; right: 0; bottom: 0;"
		>
			<u-button type="error" class="mar-r-10" @click="handleShowRejected()">驳回</u-button>
			<!-- <u-button type="warning" class="mar-r-10" @click="handleTemporarilyStore()">暂存</u-button> -->
			<u-button type="success" @click="handleAgreed()">同意</u-button>
		</view>
		
		<!-- 驳回 -->
		<CustomPopUp
			title="驳回"
			:show="bohui.show"
		>
			<view slot="content" class="pad-10">
				<view>
					<u--textarea v-model="bohui.reason" placeholder="请输入驳回原因"></u--textarea>
				</view>
				
				<view class="flex align-center justify-end mar-t-20">
					<u-button class="mar-r-10" @click.native.stop="handleCloseRejected()">取消</u-button>
					<u-button type="primary" @click.native.stop="handleRejected()">确定</u-button>
				</view>
			</view>
		</CustomPopUp>
	</view>
</template>

<script>
	import CustomRadio from '@/components/CustomRadio/index'
	import CustomPopUp from '@/components/CustomPopUp/index'
	import Utils from '@/utils/utils'
	
	import { mapState } from 'vuex'
	import { getApplyForRecordsDetail } from '@/apis/replaceApplyForRecords/index'
	import { examineOrder } from '@/apis/repairManagerExamine/index'
	import { getRepairWorkers } from '@/apis/transferApproval/transferApproval'
	
	export default {
		components: {
			CustomRadio,
			CustomPopUp,
		},
		
		computed: {
			...mapState({
				token: state => state.user.token,
				longitude: state => state.user.longitude,
				latitude: state => state.user.latitude,
			}),
		},
		
		data() {
			return {
				pageState: 1, // 1 待审核、2 已驳回、3 已同意
				repairTaskId: null, // 任务id
				orderId: null, // 订单号，上个页面传过来的
				tableData: [],
				tableData2: [],
				tableData3: [],
				tableData4: [],
				repairWorkerName: '',
				repairWorkers: [], // 维修工列表
				
				// 驳回
				bohui: {
					show: false,
					reason: '',
				}
			}
		},
		
		onLoad(options) {
			this.orderId = options.id
			this.repairTaskId = options.repairTaskId
			this.pageState = parseInt(options.pageState)
			this.init()
			this.findDetails()
			this.findRepairWorkers()
		},
		
		methods: {
			// 初始化
			init() {
				this.tableData = [
					{
						DESCR: '申请单号',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '车号',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '申请时间',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '申请人',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '申请人手机号',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '申请人所属机构',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '反馈人',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '更换原因',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}
				]
				
				this.tableData2 = [
					{
						DESCR: '机械环保代码',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更前终端型号',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更后终端型号',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更前芯片型号',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更后芯片型号',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更前芯片ID',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更后芯片ID',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '变更前终端编码',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}, {
						DESCR: '变更后终端编码',
						DESCR_CVAL: '',
						TYPE: 'textarea',
						isEdit: true,
					}
				]
				
				this.tableData3 = [
					{
						DESCR: '状态',
						DESCR_CVAL: 1,
						REASON: '',
						TYPE: 'disabled',
						isEdit: false,
					}
				]
				
				this.tableData4 = [
					{
						DESCR: '维修经理',
						DESCR_CVAL: null,
						REASON: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '技术服务部',
						DESCR_CVAL: null,
						REASON: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '质量部',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					}, {
						DESCR: '国环',
						DESCR_CVAL: '',
						TYPE: 'disabled',
						isEdit: false,
					},
				]
			},
			
			// 查询维修工列表
			findRepairWorkers() {
				let params = {
					location: this.longitude + ',' + this.latitude,
					token: this.token
				}
				
				getRepairWorkers(params).then(res => {
					this.repairWorkers = res.Result
					
					if (this.repairWorkers.length > 0) {
						let arr = this.repairWorkers.filter(item => item.Checked === 1)
						
						// 如果存在默认维修工，那么设置默认
						if (arr.length > 0) {
							this.repairWorkerName = arr[0].UserName
						}
					}
				})
			},
			
			// 切换维修经理
			handleRadioClick(name, groudId) {
				this.repairWorkers.forEach((item, index) => {
					item.Checked = 0
					
					if (item.UserName === name) {
						item.Checked = 1
						this.repairWorkerName = item.UserName
					}
				})
			},
			
			// 查询更换申请详情
			findDetails() {
				let params = {
					terminalRno: this.orderId,
					token: this.token
				}
				
				Utils.loading('查询中...')
				
				getApplyForRecordsDetail(params).then(res => {
					uni.hideLoading()
					this.showData(res.Result)
				}).catch(err => {
					uni.hideLoading()
				})
			},
			
			// 根据查到的详情，回显数据
			showData(row) {
				this.tableData[0].DESCR_CVAL = this.orderId // 申请单号
				this.tableData[1].DESCR_CVAL = row.vehicleNumber // 车号
				this.tableData[2].DESCR_CVAL = row.renewalDate // 申请时间
				this.tableData[3].DESCR_CVAL = row.applicant // 申请人
				this.tableData[4].DESCR_CVAL = row.applicantTel // 申请人手机号
				this.tableData[5].DESCR_CVAL = row.applicantOrg // 申请人所属机构
				this.tableData[6].DESCR_CVAL = row.feedbackPerson // 反馈人
				this.tableData[7].DESCR_CVAL = row.feedbackDescription // 更换原因
				this.tableData2[0].DESCR_CVAL = row.epCode // 机械环保代码
				this.tableData2[1].DESCR_CVAL = row.terminalUnitType // 变更前终端型号
				this.tableData2[2].DESCR_CVAL = row.terminalUnitTypeRenewal // 变更后终端型号
				this.tableData2[3].DESCR_CVAL = row.chipModel // 变更前芯片型号
				this.tableData2[4].DESCR_CVAL = row.chipModelRenewal // 变更后芯片型号
				this.tableData2[5].DESCR_CVAL = row.chipModelId // 变更前芯片ID
				this.tableData2[6].DESCR_CVAL = row.chipModelIdRenewal // 变更后芯片ID
				this.tableData2[7].DESCR_CVAL = row.terminalUnitNcode // 变更前终端编码
				this.tableData2[8].DESCR_CVAL = row.terminalUnitNcodeRenewal // 变更后终端编码
				this.tableData3[0].REASON = row.auditStep // 状态
			},
			
			// 显示驳回弹窗
			handleShowRejected() {
				this.bohui.show = true
			},
			
			// 驳回
			handleRejected() {
				if (!this.bohui.reason) {
					Utils.popUpMsg('none', '请填写驳回原因！', 1500)
					return
				}
				
				let params = {
					state: 2,
					token: this.token,
					reason: this.bohui.reason,
					repairWorker: this.repairWorkerName,
					id: this.repairTaskId
				}
				
				Utils.loading('驳回中...')
				
				examineOrder(params).then(res => {
					uni.hideLoading()
					Utils.popUpMsg('success', res.Message, 500)
					setTimeout(() => {
						uni.navigateBack({ delta: 1 })
					}, 500)
				}).catch(err => {
					uni.hideLoading()
				})
			},
			
			// 关闭驳回弹窗
			handleCloseRejected() {
				this.bohui.reason = ''
				this.bohui.show = false
			},
			
			// 同意
			handleAgreed() {
				Utils.modal('提示', '确定同意审核？', res => {
					if (res.confirm) {
						let params = {
							state: 1,
							token: this.token,
							repairWorker: this.repairWorkerName,
							id: this.repairTaskId
						}
						
						Utils.loading('审批中...')
						
						examineOrder(params).then(res => {
							uni.hideLoading()
							Utils.popUpMsg('success', res.Message, 500)
							setTimeout(() => {
								uni.navigateBack({ delta: 1 })
							}, 500)
						}).catch(err => {
							uni.hideLoading()
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.pages {
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		background-color: #fff;
	}
</style>
